﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ActivitiesControl.Domain.Services;
using ActivitiesControl.Domain.Utils;
using ActivitiesControl.Domain.Model;
using Entities = ActivitiesControl.Domain.Model;

namespace ActivitiesControl.GUI.Desktop.Forms
{
    public partial class ChangePasswordForm
		: Form
    {
		private string username = null;

        public ChangePasswordForm()
        {
            InitializeComponent();
        }

        public ChangePasswordForm(string username)
        {
            InitializeComponent();
            this.username = username;
			
        }

        private void textBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
                btnOk_Click(null, null);
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
			ServicesLocator.UserServices.MessageRaised += new MessageHandler(service_MessageRaised);
			ServicesLocator.UserServices.UpdateUserPassword(
				username,
				txtOldPassword.Text,
				txtNewPassword.Text,
				txtConfirmNewPassword.Text);
        }

		void service_MessageRaised(object sender, MessageEventArgs e)
		{
			MessageBox.Show(e.Message.Text, e.Message.Title, MessageBoxButtons.OK, MessageBoxIcon.Warning);

			if (e.Message.Type == MessageType.Ok)
			{
				ServicesLocator.UserServices.MessageRaised -= new MessageHandler(service_MessageRaised);
				this.Close();
				this.Dispose();
			}
		}        
    }
}
